import request from "./useRequests";
import { userinfo } from '@/stores/counter'

export default function getArticleCard() {
  const user = userinfo()
  async function singleCard(id: number, setCurrent: boolean = false) {//-1代表查询单篇文章，不存进alldetail[]
    // console.log(id)
    // console.log(setCurrent)
    // console.log(user.details)
    if (user.details[id]) {
      if (setCurrent) user.curArticle = user.details[id]
      return
    }
    try {
      const res = await request({
        method: "get",
        url: `/article/`+id,
        // params: {
        //   "id": id
        // }
      })
      if (res.data.status) {
        user.details[id] = res.data.data
        if (setCurrent) user.curArticle = res.data.data
      }
    } catch (err: any) {
      //alert(err)
    }
  }

  async function singlePic(coverId: number) {
    if (user.pics[coverId]) return
    try {
      const res = await request({
        method: "get",
        url: `/news/getPic`,
        params: {
          "id": coverId,
        }
      })
      if (res.data.success) {
        user.pics[coverId] = res.data.data
      }
    } catch (err: any) {
      //alert(err)
    }
  }

  return {
    singleCard,
    singlePic
  }
}